<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Libs\System\SystemInfoLiunx;
use App\Libs\System\SystemInfoWindows;
use App\Models\Blog\Article;
use App\Models\Blog\Comment;
use App\Models\Common\Region;
use App\Models\Expansion\Chat;
use App\Models\Expansion\EmailReceive;
use App\Models\System\Info;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;

class CharService
{
    /**
     * 首页图
     *
     * @param Request $request
     *
     * @return array
     */
    public function CharService( $request )
    {
        $start = Carbon::now()->startOfWeek()->timestamp;
        $end   = Carbon::now()->endOfWeek()->timestamp;
        $name  = [];
        $count = [];
        for ( $i = 0; $i < 7; $i++ ) {
            $s = date('Y-m-d H:i:s', strtotime("+" . $i . " day", $start));
            $e = date('Y-m-d H:i:s', strtotime("+" . ( $i + 1 ) . " day", $start));
            switch ( $request->input('category') ) {
                case 1:
                    $count[ 0 ][] = User::BetweenCountModel($s, $e, [ 'is_show' => 2, 'status' => 1 ]);
                    $count[ 1 ][] = Article::BetweenCountModel($s, $e, [ 'static' => 1, 'status' => 3 ]);
                    $count[ 2 ][] = Comment::BetweenCountModel($s, $e, [ 'static' => 1, 'status' => 1 ]);
                    $name[ 0 ]    = '用户';
                    $name[ 1 ]    = '文章';
                    $name[ 2 ]    = '评论';
                    break;
                default:
                    $count[ 0 ][] = User::BetweenCountModel($s, $e, [ 'is_show' => 1, 'status' => 1 ]);
                    $count[ 1 ][] = EmailReceive::BetweenCount($s, $e);
                    $count[ 2 ][] = Chat::BetweenCount($s, $e);
                    $name[ 0 ]    = '用户';
                    $name[ 1 ]    = '邮件';
                    $name[ 2 ]    = '聊天消息';
                    break;
            }
        }
        $data  = [
            'start' => date("Y年m月d日", $start),
            'end'   => date("Y年m月d日", $end),
            'count' => $count,
            'name'  => $name,
        ];
        $group = [];
        $user  = Info::GroupModel('province');
        $i     = 0;
        foreach ( $user as $value ) {
            if ( ! $value->province ) continue;
            $region = Region::ValueModel([ 'id' => $value->province ], 'name');
            $len    = mb_strpos($region, '省');
            if ( ! $len ) $len = mb_strpos($region, '市');
            if ( $len ) $region = mb_substr($region, 0, $len, 'utf-8');
            $group[ $i ][ 'name' ]  = $region;
            $group[ $i ][ 'value' ] = Info::CountModel([ 'province' => $value->province ]);
            $i++;
        }
        unset($user);
        $collect        = collect($group);
        $data[ 'data' ] = $collect->sortByDesc('count')->take(6)->values()->toArray();
        $data[ 'code' ] = 200;
        unset($collect);
        return $data;
    }

    /**
     * 获取系统信息
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCpuUsage()
    {
        if ( strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ) {
            $info             = new SystemInfoWindows;
            $data[ 'cpu' ]    = (int) $info->getCpuUsage();
            $memory           = $info->getMemoryUsage();
            $data[ 'memory' ] = (int) $memory[ 'usage' ];
            $data[ 'time' ]   = $info->getTimeUsage();
        }
        else $data = ( new SystemInfoLiunx )->getCpuUsage();
        return data($data);
    }
}
